package com.njlime.sys.controller;

import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.sys.entities.UnitWxConfig;
import com.njlime.sys.security.util.UserUtils;
import com.njlime.sys.service.UnitService;
import com.njlime.sys.service.UserConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 微信配置
 * Created by Dean on 2017-7-25.
 */
@Controller
@RequestMapping(value = "sys/unit/wxconfig")
public class UnitWXConfigController extends BaseController{

    @Resource
    private UnitService unitService;
    /**
     * 获取单位信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public String unitInfo(HttpServletRequest request){
        try {
          int unitId = UserUtils.getUser().getRootUnit().getId();

            return toJsonSuccessData(unitService.getWxConfig(unitId));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 编辑单位
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit" )
    public String edit(HttpServletRequest request,UnitWxConfig unitWxConfig,Integer userConfigId ){
        try {
             int unitId = UserUtils.getUser().getRootUnit().getId();

             unitWxConfig.setUnit_id(unitId);
             unitService.editUnitConfig(unitWxConfig,userConfigId);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }
}
